﻿-- =============================================
-- Author:		zengfei
-- Create date: 03m/15/2012
-- Description:	更新送货单明细表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifyShipFormDetail]
	@p_Action	char(1),
	@P_ShipFormNo	varchar(30), 
	@p_LineNo	varchar(3),
	@p_ProductNo	varchar(10),
	@p_ProductName	varchar(120),
	@p_ProductSpec	varchar(60),
	@p_ProductModel	varchar(60),
	@p_Unit	varchar(20),
	@P_Qty	numeric(10, 2),
	@p_Price	numeric(10, 2),
	@p_Amount	numeric(10, 2),
	@p_Remark	varchar(120),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into ShipFormDetail (ShipFormNo, [LineNo], ProductNo, ProductName, ProductSpec, ProductModel, Unit, Qty, Price, Amount, Remark)
		values (@p_ShipFormNo, @p_LineNo, @p_ProductNo, @p_ProductName, @p_ProductSpec, @p_ProductModel, @p_Unit, @p_Qty, @p_Price, @p_Amount, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update ShipFormDetail set ProductNo = @p_ProductNo,
								ProductName = @p_ProductName,
								ProductSpec = @p_ProductSpec,
								ProductModel = @p_ProductModel,
								Unit = @p_Unit,
								Qty = @p_Qty,
								Price = @p_Price,
								Amount = @p_Amount,
								Remark = @p_Remark 
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from ShipFormDetail where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into ShipFormDetail_log (ShipFormNo, [LineNo], ProductNo, ProductName, ProductSpec, ProductModel, Unit, Qty, Price, Amount, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_ShipFormNo, @p_LineNo, @p_ProductNo, @p_ProductName, @p_ProductSpec, @p_ProductModel, @p_Unit, @p_Qty, @p_Price, @p_Amount, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1

